class Category {
  final String id;
  final String name;
  final List<Category> children;

  Category({
    required this.id,
    required this.name,
    this.children = const [],
  });

  factory Category.fromJson(Map<String, dynamic> json) {
    List<Category> childrenList = [];
    if (json['children'] != null && json['children'] is List) {
      childrenList = (json['children'] as List)
          .map((item) => Category.fromJson(item))
          .toList();
    }

    return Category(
      id: json['id']?.toString() ?? '',
      name: json['name'] ?? '',
      children: childrenList,
    );
  }

  Map<String, dynamic> toJson() {
    return {
      'id': id,
      'name': name,
      'children': children.map((c) => c.toJson()).toList(),
    };
  }
}
